// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
// 引入页面主题
import 'element-ui/lib/theme-default/index.css'
import '../static/common.css'
import '../static/iconfont.css'

import ElementUI from 'element-ui'
import Loading from 'element-ui'
import VueResource from 'vue-resource'
import echarts from 'echarts'
import store from './vuex/store'
import csRouter from './config/routes'
import API_ROOT from './config/env'
import TASK_API from './config/taskapi'

import PageConfig from './config/PageConfig'
/*引入 common.css*/
import '../static/common.css'

import moment from 'moment'


// 自定义插件
import tagCom from './plugin/base/tags'
Vue.use(tagCom)
import tagList from './plugin/bus/tagList'
Vue.use(tagList)
import customer from './plugin/bus/customer'
Vue.use(customer)
import concat from './plugin/bus/concat'
Vue.use(concat)
import hisConcat from './plugin/bus/hisConcat'
Vue.use(hisConcat)
import questionTree from './plugin/bus/questionTree'
Vue.use(questionTree)
import questionPreView from './plugin/bus/questionPreView'
Vue.use(questionPreView)
import questionOrder from './plugin/bus/questionOrder'
Vue.use(questionOrder)
import questionFinal from './plugin/bus/questionFinal'
Vue.use(questionFinal)
    //定义 URL 公共地址
Vue.prototype.API_ROOT = API_ROOT
Vue.prototype.TASK_API = TASK_API
    //定义分页公共数据
Vue.prototype.page = PageConfig

Vue.prototype.moment = moment;
Vue.prototype.echarts = echarts;
//
//全局导航
csRouter.beforeEach(function(to, from, next) {

    if (to.path != "/login") {
        //验证是否登录
        var token = window.localStorage.getItem("token");
        if (token == undefined || token.length < 30) {
            next("/login");
        }
        //远程校验

    }
    next();
})

Vue.use(VueResource)
    // 请求前全局赋值
Vue.http.interceptors.push(function(request, next) {
        // 此处this为请求所在页面的Vue实例
        // modify request
        if (!request.params) {
            request.params = {}
        }
        request.params.uid = window.localStorage.getItem('uid')
        request.params.token = window.localStorage.getItem('token')

        if (request.params.start != null && undefined != request.params.start) {
            request.params.start = (request.params.start - 1) * PageConfig.pageSize;
        }


        // continue to next interceptor    　　
        next(function(response) {
            //      // 在响应之后传给then之前对response进行修改和逻辑判断。对于token时候已过期的判断，就添加在此处，页面中任何一次http请求都会先调用此处方法    　　　　　　
            //      if (response.status == 0) {
            //          csRouter.push("/login");
            //      }
            //      var data = response.data;
            //      if (data == null) {
            //          csRouter.push("/login");
            //          this.$message.error("请先登录");
            //          return;
            //      }
            //      //未登录
            //      if (!data.success && data.exType == "unLogin") {
            //          csRouter.push("/login");
            //          this.$message.error(data.msg);
            //
            //          return;
            //      }
            //      //无权访问
            //      if (!data.success && data.exType == "unAuth") {
            //          this.$message.error(data.msg);
            //
            //          return;
            //      }

            return response;

        })
    })
    // 启用 UI 库
Vue.use(ElementUI)

//过滤器
Vue.filter('moment', function(value, formatString) {
    if (value == undefined || value == null || value == "") {
        return "";
    }
    value = new Number(value);
    return moment(value).format('YYYY-MM-DD HH:mm:ss');
});


// 启动应用
/* eslint-disable no-new */
new Vue({
        router: csRouter,
        store: store
    }).$mount('#app')
    // 转向登录
csRouter.replace('/login')